class WordDefinitionsController < ApplicationController

  def search_ajax
    render :nothing => true and return unless request.xhr?

    w = params[:word] ? params[:word][:text].to_s.downcase : ''

    if w.blank?
      @div = :din_content
      @class_name = WordDefinition.class_name
      render :template => 'shared/error_list' and return
    end

    @word = Word.find_or_create_by_text(w)
    @word_definitions = WordDefinition.for(@word, nil,
                                            :force_look_up => true,
                                            :page => params[:page])
    @word.delete if @word_definitions.empty?
  end

  def search
    render :nothing => true and return unless request.post?

    w = params[:word][:text].to_s.downcase
    if w.blank?
      flash[:error] = 'Search expression is missing!'
      redirect_to :back and return
    end    

    @word = Word.find_or_create_by_text(w)
    @word_definitions = WordDefinition.for(@word, nil,
                                          :force_look_up => true,
                                          :page => params[:page],
                                          :limit => 1)
    @word.delete if @word_definitions.empty?
  end

end
